const http = require('http');

const server = http.createServer();

// 存储在变量中 => 内存
let currentId = 2;
let users = [
    {
        "id": 1,
        "username": "DaHai"
    },
    {
        "id": 2,
        "username": "zMouse"
    }
];

server.on('request', async(req, res) => {

    if (req.url == '/users') {
        res.setHeader('Content-Type', 'application/json;charset="utf-8"');
        res.end(JSON.stringify(users));
    } else if (req.url == '/add') {
        users.push({
            id: ++currentId,
            username: `test_${Math.random()}`
        });
        res.setHeader('Content-Type', 'text/html;charset="utf-8"');
        res.end('添加成功');
    }
});

server.listen(8888);
